vimfandomcom-20200223-history
Highlight all search pattern matches
When searching, it is often helpful to highlight all search hits (in a program, for example, that allows you to quickly see all occurrences of a variable). This tip shows how search highlighting is controlled, and has a method to highlight searches without moving. Highlighting search matches To highlight all search matches, set the following option: :set hlsearch With the defaults, setting this option causes all text matching the current search to be highlighted using the Search highlight group, which adds a yellow background to the current highlighting. See , or type :hi Search to see what color you have it set to. You can easily change the default highlighting with, for example, :hi Search guibg=LightBlue. To disable the highlighting temporarily, enter (this is a command, not an option): :nohlsearch This command (which can be abbreviated to :noh) removes the highlighting for the current search. The highlighting returns for the next search. If you do this often, put a mapping in your vimrc, like this: " Press Space to turn off highlighting and clear any message already displayed. :nnoremap :nohlsearch:echo To disable highlighting completely, even after a subsequent search, use: :set nohlsearch If you want to be able to enable/disable highlighting quickly, you can map a key to toggle the hlsearch option: " Press F4 to toggle highlighting on/off, and show current value. :noremap :set hlsearch! hlsearch? Or, press return to temporarily get out of the highlighted search. :nnoremap :nohlsearch Highlighting can be enabled on Vim startup, when reading the viminfo file. Add the following to your vimrc if you want Vim to start with no search highlighting: :set viminfo^=h Highlight matches without moving It can be useful to highlight the word under the cursor like *, but without jumping to the next match. Then you can see the search highlights on the current screen, without any scrolling. Move to the first match (ggn), last (GN), next (n) or previous (N) as usual. One procedure would be to type *`` (search for next occurrence of word, then jump back to the original position). Following is another procedure that won't flicker the screen when the search would require scrolling. The technique is to assign the wanted pattern to the search register (@/), and to set the 'hlsearch' option (abbreviated as 'hls'). For example, these commands highlight every whole word matching "the": :let @/='\' :set hls The following map uses the above technique so that pressing F8 will highlight all occurrences of the current word: :nnoremap :let @/='\<=expand("")\>':set hls Here is how to do the same for visually selected text: set guioptions+=a function! MakePattern(text) let pat = escape(a:text, '\') let pat = substitute(pat, '\_s\+$', '\\s\\*', ) let pat = substitute(pat, '^\_s\+', '\\s\\*', '') let pat = substitute(pat, '\_s\+', '\\_s\\+', 'g') return '\\V' . escape(pat, '\"') endfunction vnoremap :let @/="=MakePattern(@*)":set hls The script starts by adding the 'a' flag to 'guioptions' so that visually selecting text automatically places the text in the clipboard (register *) – that only works on some systems ( ). The function replaces all whitespace strings (space, tab, newline) with a pattern that finds any amount of whitespace. For example, selecting "foo bar" and pressing F8 will highlight each of the two occurrences in: "foo bar" and "foo bar" Another approach is to use the following to map the Enter key () so that pressing Enter toggles highlighting for the current word on and off: let g:highlighting = 0 function! Highlighting() if g:highlighting 1 && @/ =~ '^\\<'.expand().'\\>$' let g:highlighting = 0 return ":silent nohlsearch\" endif let @/ = '\<'.expand('').'\>' let g:highlighting = 1 return ":silent set hlsearch\" endfunction nnoremap Highlighting() After sourcing the above, move the cursor to a word then press Enter to highlight all occurrences of that word (the cursor is not moved). Press Enter again to clear the highlighting. It works by setting the search register so pressing n or N will search forwards or backwards for the word. The mapping (see ) means that the text returned by Highlighting() is executed as a command. That is necessary because commands that affect the current search highlighting are ignored when executed inside a function. The global variable g:highlighting is used to track whether highlighting is active as there appears to be no way to detect that using Vim script. The operator =~ is used to check whether the contents of the search register (@/) matches the pattern to search for the exact current word; it uses a regular expression for the match so that the current setting of 'ignorecase' is used (when set, highlighting a word like "example" will also highlight "Example"). See also *Searching *Search for visually selected text *Highlight multiple words *Auto highlight current word when idle References * option to control search highlighting * command to temporarily remove search highlighting * include h to disable the effect of 'hlsearch' * l:Search means the Search highlight group is used for matches * you can highlight more than the search pattern Comments I like this no-flicker find/highlight a lot. I used this one but wished it put my cword into search history so / would recall it. :nnoremap :let @/='\<=expand("")\>':set hls So I discovered and tried histadd and it worked!?! :nnoremap :let curwd='\\\<=expand("")\\\>' :let @/=curwd:call histadd("search", curwd):set hls Thank you so much for the hint.